import request from '@/utils/request'
envConfig.headerUploadPath = process.env.VUE_APP_URL + process.env.VUE_APP_HEADER_PATH
export function page(name, state, begin, end, page, pageSize) {
  return request({
    url: process.env.VUE_APP_PATH + '/admins?name=' + name + '&state=' + state + '&begin=' + begin + '&end=' + end + '&page=' + page + '&pageSize=' + pageSize,
    method: 'get'
  }).then(res => {
    const data = res.data
    for (const i in data.data.rows) {
      if (data.data.rows[i]['header']) {
        data.data.rows[i]['header'] = envConfig.headerUploadPath + data.data.rows[i]['header']
      }
    }
    return res
  })
}

export function add(admin) {
  return request({
    url: process.env.VUE_APP_PATH + '/admins',
    method: 'post',
    data: admin
  })
}

export function update(id, admin, uploadList) {
  return request({
    url: process.env.VUE_APP_PATH + '/admins',
    method: 'put',
    data: {
      admin: admin,
      fileList: uploadList,
      id: id
    }
  })
}

export function selectById(id) {
  return request({
    url: process.env.VUE_APP_PATH + '/admins/' + id,
    method: 'get'
  }).then(res => {
    const data = res.data.data[0]
    res.data.data = data
    if (data.header) {
      res.header = envConfig.headerUploadPath + data.header
    }
    return res
  })
}

export function deleteById(id) {
  return request({
    url: process.env.VUE_APP_PATH + '/admins/' + id,
    method: 'delete'
  })
}
